package linuxbox.filelib.taglib;

import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import javax.servlet.http.*;
import java.util.*;

import linuxbox.filelib.logic.*;
import linuxbox.BoxStatic;
/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: </p>
 * @author unascribed
 * @version 1.0
 */

public class FolderList extends BodyTagSupport
{
  protected int currentFolder = FileLibHome.ROOT;
  protected int rootFolder = FileLibHome.ROOT;
  protected int userid = 0;
  protected int[] showFolders=null;
  protected JspWriter out=null;
//  protected PrintStream out=null;

  protected static FileLibHome home = FileLibHome.getInstance();

  protected void outputSingleFolder(FileLibEntry entry) throws Exception
  {
    out.print("<img src=\"");
    out.print(BoxStatic.imageDir);
    out.print("node.gif");
    out.print("\">");
    if ((entry==null && currentFolder!=home.ROOT)
        ||(entry!=null && currentFolder!=entry.getID()))
    {
      out.print("<a href=\"");
      out.print(BoxStatic.servletDir);
      out.print("FolderContentAction?FOLDERID=");
      out.print(entry.getID());
      out.print("\">");
      out.print("<img src=\"");
      out.print(BoxStatic.imageDir);
      out.print("folder.gif\" border=0 HSPACE=2 VSPACE=1>");
      if (entry == null)
        out.print("Root");
      else
        out.print(entry.getName());
      out.print("</a>");
    }
    else
    {
      out.print("<img src=\"");
      out.print(BoxStatic.imageDir);
      out.print("folder.gif\" border=0 HSPACE=2 VSPACE=1>");
      out.print("<SPAN class=onfolder><B>");
      out.print("<a href=\"");
      out.print(BoxStatic.servletDir);
      out.print("FolderContentAction?FOLDERID=");
      out.print(entry.getID());
      out.print("\">");
      out.print(entry.getName());
      out.print("</a>");
      out.print("</B></SPAN>");
    }
    out.print("<br>");
  }

  protected void outputFolderWithChildren(FileLibEntry entry) throws Exception
  {
    if (entry == null)
    {
      out.print("<A href=\"javascript:onExtract('F");
      out.print(home.ROOT);
      out.print("',0)\"><img name=\"imgF");
      out.print(home.ROOT);
      out.print("\" src=\"");
      out.print(BoxStatic.imageDir);
      out.print("minus.gif\" border=0> </A> ");
      if (currentFolder != home.ROOT)
      {
        out.print("<a href=\"");
        out.print(BoxStatic.servletDir);
        out.print("FolderContentAction?FOLDERID=");
        out.print(home.ROOT);
        out.print("\"> ");
        out.print(" <img src=\"");
        out.print(BoxStatic.imageDir);
        out.print("folder.gif\" border=0 HSPACE=2 VSPACE=1 >");
        out.print("Root");
        out.print("</A>");
      }
      else
      {
        out.print(" <img src=\"");
        out.print(BoxStatic.imageDir);
        out.print("folder.gif\" border=0 HSPACE=2 VSPACE=1 >");
        out.print("<SPAN class=onfolder><B>");
        out.print("<a href=\"");
        out.print(BoxStatic.servletDir);
        out.print("FolderContentAction?FOLDERID=");
        out.print(home.ROOT);
        out.print("\">");
        out.print("Root");
        out.print("</a>");
        out.print("</B></SPAN>");
      }
      out.print("<BR><div id=\"F");
      out.print(home.ROOT);
      out.print("\" class=treenode >");
      showFolders(home.ROOT);
    }
    else
    {
      boolean showFolder = false;
      if (entry.getID() == currentFolder)
        showFolder = true;
      if (!showFolder && showFolders != null)
      {
        for(int i=0; i<showFolders.length; i++)
        {
          if (entry.getID() == showFolders[i])
          {
            showFolder = true;
            break;
          }
        }
      }
      out.print("<A href=\"javascript:onExtract('F");
      out.print(entry.getID());
      out.print("',0)\"><img name=\"imgF");
      out.print(entry.getID());
      out.print("\" src=\"");
      out.print(BoxStatic.imageDir);
      if (showFolder)
        out.print("minus.gif\"");
      else
        out.print("plus.gif\"");
      out.print(" border=0> </A> ");
      if (currentFolder != entry.getID())
      {
        out.print("<a href=\"");
        out.print(BoxStatic.servletDir);
        out.print("FolderContentAction?FOLDERID=");
        out.print(entry.getID());
        out.print("\">  <img src=\"");
        out.print(BoxStatic.imageDir);
        out.print("folder.gif\" border=0 HSPACE=2 VSPACE=1 >");
        out.print(entry.getName());
        out.print("</A>");
      }
      else
      {
        out.print("<img src=\"");
        out.print(BoxStatic.imageDir);
        out.print("folder.gif\" border=0 HSPACE=2 VSPACE=1 >");
        out.print("<SPAN class=onfolder><B>");
        out.print("<a href=\"");
        out.print(BoxStatic.servletDir);
        out.print("FolderContentAction?FOLDERID=");
        out.print(entry.getID());
        out.print("\">");
        out.print(entry.getName());
        out.print("</A>");
        out.print("</B></SPAN>");
      }
      out.print("<BR><div id=\"F");
      out.print(entry.getID());
      out.print("\" class=treenode ");
      if (!showFolder)
        out.print("style=\"display:none;\"");
      out.print(">");
      showFolders(entry.getID());
    }
    out.print("</div>");
  }

  protected void showFolders(int curFolder) throws Exception
  {
/*
    <a href="javascript:onExtract('F53', 0)">
          <img name="imgF53" src="/s360v2/plus.gif" border=0>
        </a>
        <a href="s360.exe?page=FileIndex&fid=53">
          <img src="/s360v2/folder.gif" border=0 HSPACE=2 VSPACE=1>33333333333
        </a>
        <br>
        <div id="F53" class=treenode style="display:none;">
                <img src="/s360v2/node.gif"><a href="s360.exe?page=FileIndex&fid=111"><img src="/s360v2/folder.gif" border=0 HSPACE=2 VSPACE=1>333aaa</a><br>
        </div>
*/

    Collection col = home.getFolders(userid, curFolder,home.BROWSE_ALLOW);
    Iterator it = col.iterator();
    if (it.hasNext())
    {
      while (it.hasNext())
      {
        FileLibEntry entry = (FileLibEntry) it.next();
        if (home.hasChildFolder(userid,entry.getID(),home.BROWSE_ALLOW))
        {
          outputFolderWithChildren(entry);
/*
          out.print("<A href=\"javascript:onExtract('F");
          out.print(entry.getID());
          out.print("',0)\"><img name=\"imgF");
          out.print(entry.getID());
          out.print("\" src=\"");
          out.print(BoxStatic.imageDir);
          out.print("/plus.gif\" border=0> </A> <a href=\"ShowFolder\">  <img src=\"");
          out.print(BoxStatic.imageDir);
          out.print("folder.gif\" border=0 HSPACE=2 VSPACE=1 >");
          out.print(entry.getName());
          out.print("</A> <BR><div id=\"F");
          out.print(entry.getID());
          out.print("\" class=treenode style=\"display:none;\">");
          //show child folders
          showFolders(entry.getID());
          out.print("</div>");
*/
        }
        else
        {
          outputSingleFolder(entry);

//          <img src="/s360v2/node.gif"><a href="s360.exe?page=FileIndex&fid=53"><img src="/s360v2/folder.gif" border=0 HSPACE=2 VSPACE=1>33333333333</a><br>
/*          out.print("<img src=\"");
          out.print(BoxStatic.imageDir);
          out.print("node.gif");
          out.print("\"><a href=\"showFolder\"");
          out.print("><img src=\"");
          out.print(BoxStatic.imageDir);
          out.print("folder.gif\" border=0 HSPACE=2 VSPACE=1>");
          out.print(entry.getName());
          out.print("</a><br>");
*/        }
      }
    }
  }
  public int doStartTag() throws JspException
  {
    out = pageContext.getOut();
//    out = System.out;
    try
    {
      FileLibEntry entry = null;
      if (rootFolder != home.ROOT)
        entry = home.get(rootFolder);
      showFolders = home.getParentFolders(currentFolder);
      if (home.hasChildFolder(userid,rootFolder,home.BROWSE_ALLOW))
      {
        outputFolderWithChildren(entry);
      }
      else //single node
      {
        outputSingleFolder(entry);
      }
      out.flush();
    }
    catch(Exception e)
    {
      e.printStackTrace();
      throw new JspException(e.getMessage());
    }

    return SKIP_BODY;
  }

  public void setUserid(int aValue)
  {
    userid = aValue;
  }

  public void setCurrentFolder(int aValue)
  {
    currentFolder = aValue;
  }
  public void setRootFolder(int aValue)
  {
    rootFolder = aValue;
  }
}
